<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- mkdir | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- mkdir</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux mkdir 命令用来创建指定的名称的目录，要求创建目录的用户在当前目录中具有写权限，并且指定的目录名不能是当前目录中已有的目录。      </p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir [选项] [目录]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过 mkdir 命令可以实现在指定位置创建以 DirName(指定的文件名)命名的文件夹或目录。要创建文件夹或目录的用户必须对所创建的文件夹的父文件夹具有写权限。并且，所创建的文件夹(目录)不能与其父目录(即父文件夹)中的文件名重名，即同一个目录下不能有同名的(区分大小写)。 </p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-m, –mode=模式，设定权限&lt;模式&gt; (类似 chmod)，而不是 rwxrwxrwx 减 umask  </li>
<li>-p, –parents  可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;   </li>
<li>-v, –verbose  每次创建新目录都显示信息     <ul>
<li>–help   显示此帮助信息并退出        </li>
<li>–version  输出版本信息并退出    </li>
</ul>
</li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：创建一个空目录"><a href="#实例1：创建一个空目录" class="headerlink" title="实例1：创建一个空目录"></a>实例1：创建一个空目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir test1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost soft]<span class="comment"># cd test</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir test1</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 4drwxr-xr-x 2 root root 4096 10-25 17:42 test1</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例2：递归创建多个目录"><a href="#实例2：递归创建多个目录" class="headerlink" title="实例2：递归创建多个目录"></a>实例2：递归创建多个目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir -p test2/test22</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir -p test2/test22</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 8drwxr-xr-x 2 root root 4096 10-25 17:42 test1</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test2/</span></div><div class="line">[root@localhost test2]<span class="comment"># ll</span></div><div class="line">总计 4drwxr-xr-x 2 root root 4096 10-25 17:44 test22</div><div class="line">[root@localhost test2]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：创建权限为777的目录"><a href="#实例3：创建权限为777的目录" class="headerlink" title="实例3：创建权限为777的目录"></a>实例3：创建权限为777的目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir -m 777 test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir -m 777 test3</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 12drwxr-xr-x 2 root root 4096 10-25 17:42 test1</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;test3 的权限为rwxrwxrwx</p>
<h3 id="实例4：创建新目录都显示信息"><a href="#实例4：创建新目录都显示信息" class="headerlink" title="实例4：创建新目录都显示信息"></a>实例4：创建新目录都显示信息</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir -v test4</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir -v test4</span></div><div class="line">mkdir: 已创建目录 “test4”</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir -vp test5/test5-1</span></div><div class="line">mkdir: 已创建目录 “test5”</div><div class="line">mkdir: 已创建目录 “test5/test5-1”</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：一个命令创建项目的目录结构"><a href="#实例5：一个命令创建项目的目录结构" class="headerlink" title="实例5：一个命令创建项目的目录结构"></a>实例5：一个命令创建项目的目录结构</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考：<a href="http://www.ibm.com/developerworks/cn/aix/library/au-badunixhabits.html" target="_blank" rel="external">UNIX 高手的 10 个习惯</a> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mkdir -vp scf/&#123;lib/,bin/,doc/&#123;info,product&#125;,logs/&#123;info,product&#125;,service/deploy/&#123;info,product&#125;&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mkdir -vp scf/&#123;lib/,bin/,doc/&#123;info,product&#125;,logs/&#123;info,product&#125;,service/deploy/&#123;info,product&#125;&#125;</span></div><div class="line">mkdir: 已创建目录 “scf”</div><div class="line">mkdir: 已创建目录 “scf/lib”</div><div class="line">mkdir: 已创建目录 “scf/bin”</div><div class="line">mkdir: 已创建目录 “scf/doc”</div><div class="line">mkdir: 已创建目录 “scf/doc/info”</div><div class="line">mkdir: 已创建目录 “scf/doc/product”</div><div class="line">mkdir: 已创建目录 “scf/logs”</div><div class="line">mkdir: 已创建目录 “scf/logs/info”</div><div class="line">mkdir: 已创建目录 “scf/logs/product”</div><div class="line">mkdir: 已创建目录 “scf/service”</div><div class="line">mkdir: 已创建目录 “scf/service/deploy”</div><div class="line">mkdir: 已创建目录 “scf/service/deploy/info”</div><div class="line">mkdir: 已创建目录 “scf/service/deploy/product”</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tree scf/</span></div><div class="line">scf/</div><div class="line">|-- bin</div><div class="line">|-- doc</div><div class="line">|   |-- info</div><div class="line">|   `-- product</div><div class="line">|-- lib</div><div class="line">|-- logs</div><div class="line">|   |-- info</div><div class="line">|   `-- product</div><div class="line">`-- service</div><div class="line">   	 	`-- deploy</div><div class="line">  	    	|-- info</div><div class="line">        	`-- product</div><div class="line">12 directories, 0 files</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/3. Linux 命令-mkdir/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
